﻿package com.moonshineproject.plugin.globalerrorhandling {
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	public class GEHError extends Sprite {
		public static const REPOSITION:String = "reposition";
		public static const SELECTED:String = "selected";
		public static const REMOVE_ERROR:String = "removeError";
		
		public var title:String;
		public var text:String;
		
		private static const BUTTON_HEIGHT:Number = 22;
		private static const TEXT_HEIGHT:Number = 200;
		private static const CLOSE_WIDTH:Number = 22;
		
		public var w:Number;
		public var h:Number;
		
		private var titleBtn:Sprite = new Sprite();
		private var titleTxt:TextField = new TextField();
		
		private var highlight:Sprite = new Sprite();
		
		private var textContainer:Sprite = new Sprite();
		private var textTxt:TextField = new TextField();
		
		private var closeBtn:Sprite = new Sprite();
		
		public var textHeight:Number = 0;
		private var opened:Boolean = false;
		
		public function GEHError(title:String, text:String, w:Number) {
			this.title = title;
			this.text = text;
			this.w = w;
			
			titleBtn.mouseChildren = false;
			titleBtn.buttonMode = true;
			titleBtn.addEventListener(MouseEvent.CLICK, toggleText);
			addChild(titleBtn);
			
			TweenNano.to(highlight, 3, { "delay": 5, "alpha": 0, "onComplete": destroyHighlight } );
			titleBtn.addChild(highlight);
			
			closeBtn.buttonMode = true;
			closeBtn.addEventListener(MouseEvent.CLICK, removeError);
			addChild(closeBtn);
			
			titleTxt.defaultTextFormat = new TextFormat("Arial", 12, 0x737373);
			titleTxt.antiAliasType = AntiAliasType.ADVANCED;
			titleTxt.x = 4;
			titleTxt.y = 2;
			titleTxt.height = BUTTON_HEIGHT-titleTxt.y*2;
			titleTxt.text = title;
			titleTxt.selectable = false;
			titleBtn.addChild(titleTxt);
			
			textContainer.y = BUTTON_HEIGHT;
			addChild(textContainer);
			
			textTxt.defaultTextFormat = new TextFormat("Arial", 12, 0x727272);
			textTxt.antiAliasType = AntiAliasType.ADVANCED;
			textTxt.x = 4;
			textTxt.y = 2;
			textTxt.multiline = true;
			textTxt.text = text;
			textTxt.type = TextFieldType.INPUT;
			textContainer.addChild(textTxt);
			
			resize();
		}
		
		private function destroyHighlight():void {
			highlight.graphics.clear();
			titleBtn.removeChild(highlight);
			highlight = null;
		}
		
		private function removeError(e:MouseEvent):void {
			dispatchEvent(new Event(REMOVE_ERROR));
		}
		
		public function select():void {
			if (!opened) {
				opened = true;
				TweenNano.to(this, 0.2, { "overwrite": true, "textHeight": TEXT_HEIGHT, "onUpdate": resize } );
				//TweenNano.to(titleBtn, 0.2, { "overwrite": true, "y": 7 } );
				//TweenNano.to(textContainer, 0.2, { "overwrite": true, "y": BUTTON_HEIGHT+5 } );
			}
		}
		public function deselect():void {
			if (opened) {
				opened = false;
				TweenNano.to(this, 0.2, { "overwrite": true, "textHeight": 0, "onUpdate": resize } );
				//TweenNano.to(titleBtn, 0.2, { "overwrite": true, "y": 2 } );
				//TweenNano.to(textContainer, 0.2, { "overwrite": true, "y": BUTTON_HEIGHT } );
			}
		}
		private function toggleText(e:MouseEvent):void {
			if (opened) {
				deselect();
			} else {
				select();
				dispatchEvent(new Event(SELECTED));
			}
		}
		
		public function resize(w:Number = NaN):void {
			if (!isNaN(w)) {
				this.w = w;
			} else {
				w = this.w;
			}
			
			
			closeBtn.x = w-CLOSE_WIDTH;
			titleTxt.width = w-titleTxt.x*2;
			textTxt.width = w-textTxt.x*2;
			textTxt.height = Math.max(0, textHeight-textTxt.y*2);
			h = BUTTON_HEIGHT+textHeight;
			//h = BUTTON_HEIGHT+textHeight+(opened ? 12 : 0);
			
			
			var g:Graphics;
			var m:Matrix;
			
			g = titleBtn.graphics;
			g.clear();
			m = new Matrix();
			m.createGradientBox(w, BUTTON_HEIGHT, Math.PI*0.5);
			g.lineStyle(1, 0xCFCFCF, 1, true);
			g.beginGradientFill(GradientType.LINEAR, [0xE8E8E8, 0xDFDFDF], [1, 1], [0, 255], m);
			if (opened) {
				g.drawRoundRectComplex(0, 0, w, BUTTON_HEIGHT, 6, 6, 0, 0);
			} else {
				g.drawRoundRectComplex(0, 0, w, BUTTON_HEIGHT, 6, 6, 6, 6);
			}
			g.endFill();
			
			if (highlight) {
				g = highlight.graphics;
				g.clear();
				g.beginFill(0xFFFF00, 0.5);
				if (opened) {
					g.drawRoundRectComplex(0, 0, w, BUTTON_HEIGHT, 6, 6, 0, 0);
				} else {
					g.drawRoundRectComplex(0, 0, w, BUTTON_HEIGHT, 6, 6, 6, 6);
				}
				g.endFill();
			}
			
			
			g = closeBtn.graphics;
			g.clear();
			m = new Matrix();
			m.createGradientBox(CLOSE_WIDTH, BUTTON_HEIGHT, Math.PI*0.5);
			g.lineStyle(1, 0xCACACA, 1, true);
			g.beginGradientFill(GradientType.LINEAR, [0xDADADA, 0xD3D3D3], [1, 1], [0, 255], m);
			if (opened) {
				g.drawRoundRectComplex(0, 0, CLOSE_WIDTH, BUTTON_HEIGHT, 0, 6, 0, 0);
			} else {
				g.drawRoundRectComplex(0, 0, CLOSE_WIDTH, BUTTON_HEIGHT, 0, 6, 0, 6);
			}
			g.endFill();
			
			g.lineStyle(3, 0xB5B5B5);
			g.moveTo(8, 9);
			g.lineTo(CLOSE_WIDTH-8, BUTTON_HEIGHT-7);
			g.moveTo(CLOSE_WIDTH-8, 9);
			g.lineTo(8, BUTTON_HEIGHT-7);
			
			g = textContainer.graphics;
			g.clear();
			
			if (textHeight > 0) {
				m = new Matrix();
				m.createGradientBox(w, textHeight, Math.PI*0.5);
				g.lineStyle(1, 0xCFCFCF, 1, true);
				g.beginGradientFill(GradientType.LINEAR, [0xF3F3F3, 0xEFEFEF], [1, 1], [0, 255], m);
				g.drawRoundRectComplex(0, 0, w, textHeight, 0, 0, 6, 6);
				g.endFill();
			}
			
			dispatchEvent(new Event(REPOSITION));
		}
	}
}